"""全局存储空间"""

import sys
import os
import builtins
import config
from lib.modulemanage import ModuleManage
from lib.mongo import MongoDB
from lib.redis import Redis
from lib.common import Common
from server.rpc.rpcmanage import RpcManage
from server.rpc.server import RpcServer
from typing import Optional
from model import ModelManage


# 获取根目录路径
def get_root():
    abs_path = os.path.abspath(__file__)
    abs_path = abs_path.replace('\\', '/')
    arr = abs_path.split('/g/')
    return arr[0]


ROOT = get_root()
sys.path.append(ROOT + '/game/')
sys.path.append(ROOT + '/lib/')
sys.path.append(ROOT + '/server/')
sys.path.append(ROOT + '/server/gateway')
sys.path.append(ROOT + '/server/rpc')
sys.path.append(ROOT + '/server/worker')
sys.path.append(ROOT)

C = Common()
Model: ModelManage = ModelManage()
m = ModuleManage(ROOT)


# 深拷贝
def deepCopy(src):
    if isinstance(src, dict):
        dst = {}
        for k, v in src.items():
            dst[k] = deepCopy(v)
        return dst

    elif isinstance(src, (list, tuple)):
        dst = []
        for i in src:
            dst.append(deepCopy(i))
        return dst
    else:
        return src


# 重写print
def reset_print():
    _oldPrint = builtins.print

    def print(message, *args, sep=' ', end='\n', file=None):
        if str(message).find("\n") == -1:
            # _oldPrint(f"[{time.strftime('%Y-%m-%d %X')}] ", sep=sep, end="", file=file)
            _oldPrint(f"[{C.DATE(many=True)}] ", sep=sep, end="", file=file)

        _oldPrint(message, *args, sep=sep, end=end, file=file)
        sys.stdout.flush()

    builtins.print = print


reset_print()


# 获取游戏版本
def get_game_ver():
    return config.Ver


# 获取跨服游戏版本
def get_cross_game_ver():
    return config.CrossVer


rpcServer: Optional[RpcServer] = None
rpcManage: Optional[RpcManage] = None
workerAddress: Optional[str] = None
mdb: Optional[MongoDB] = None
cross_mdb: Optional[MongoDB] = None
redis: Optional[Redis] = None
cross_redis: Optional[Redis] = None


# 初始化
def init():
    global mdb
    global redis

    mdb = MongoDB(config.MongoConfig)
    redis = Redis(config.RedisConfig, pre_key=f"s{config.ServerInfo['sid']}_")


if os.name == "nt":
    init()

if __name__ == '__main__':
    pass
